import ExtractData from './ExtractData'
import ExamComponent from './ExamComponent'
import { ref } from 'vue';
export default class FusionCalibrationComponent {
    private static instance: FusionCalibrationComponent = new FusionCalibrationComponent();
    /**
     * 融合标定
     */
    public static get Default(): FusionCalibrationComponent {
        return this.instance;
    }
    // 坐标标定答案
    public coordAnswer = ref([])
    // 隐藏
    public HiddenDomain = ref(false)
    //任务数据
    private taskConfigMap = new Map()
    //坐标数据
    public coordConfigMap = new Map()
    //标定数据
    private calibrationConfigMap = new Map()
    //融合标定数据
    public fusionCalibrationConfigModel = {
        Title : "无人驾驶系统融合标定",
        Task :{Title:"标定任务",Content:[{
            Id:0,
            Task:'',
            Type:''
        }]},
        Coord :{Title:"空间位置坐标",Content:[{
            Id:0,
            Title:'',
            Attribute:'',
            Positon:'',
            Precision: '',
            Rotation: 0,
        }]},
        Calibration:[{
            Coord: [{
                Answer: "",
                Attribute: "Tx",
                Id: 0,
                State: 0,
                Task: "",
                Title: "",
                Type: "坐标标定",
                Value: "",
                precision: "0",
            }],
            Posture: [{
                Answer: "",
                Attribute: "Rx",
                Id: 0,
                State: 0,
                Task: "",
                Title: "",
                Type: "姿态标定",
                Value: "",
                precision: "",
            }],
            Title: "",
        }]}

    //初始化
    public Initialize = ()=>{
        try{
            //加载任务数据
            let taskDate = ExtractData.Default.Extract('TaskConfig')
            taskDate.forEach((item: any) => {
                let config = JSON.parse(item)
                // console.log('config',config);
                
                this.taskConfigMap.set(config.Id, config)
                
            });
            // for(const [key,value] of this.taskConfigMap) {
            //     //@ts-ignore
            //     ExamComponent.Default.Context.Configs[0].Settings.forEach((element:any) => {
            //         if(key == element) {
            //             console.log('key:',key,'element:',element);
                        
            //             ExamComponent.Default.FuCaliTask.value.push(value.Task)
            //         }
            //     });
            // }            
            
            //加载坐标数据
            let coordDate = ExtractData.Default.Extract('CoordConfig')
            coordDate.forEach((item: any) => {
                let config = JSON.parse(item)
                this.coordConfigMap.set(config.Id, config)
            });
            

            //加载标定数据
            let calibrationDate = ExtractData.Default.Extract('CalibrationConfig')
            calibrationDate.forEach((item: any) => {
                let config = JSON.parse(item)
                if(!this.calibrationConfigMap.has(config.Task)){
                    //创建以任务ID为key的标定数数据
                    this.calibrationConfigMap.set(config.Task, {Title:config.Title,Coord:[{}],Posture:[{}]})
                }
                //将同任务Id的标定对象存储进数组
                if(config.Type == "坐标标定")
                {
                    this.calibrationConfigMap.get(config.Task).Coord.push(config)
                }else
                {
                    this.calibrationConfigMap.get(config.Task).Posture.push(config)
                }
            });
            // console.log('this.calibrationConfigMap',this.calibrationConfigMap.get(1));
            
            console.log(`%c融合标定模块初始化成功`, 'color: #00ff00');
        }
        catch (e) {
            console.log(`%cc融合标定模块初始化失败`, 'color: red', e);
        }
    }

    //设置任务
    public SetTask = (data:any) => {
        //解析任务设定
        console.log('data',data);
        
        // let taskIds = data.Task.split(',')
        let taskIds = data
        taskIds.forEach((id: any) => {
            //将设定的任务写入融合标定数据
            this.fusionCalibrationConfigModel.Task.Content.push(this.taskConfigMap.get(Number(id)))
            //根据任务设定初始化标定数据
            this.fusionCalibrationConfigModel.Calibration.push(this.calibrationConfigMap.get(String(id)))
        });
        
        // //解析坐标设定
        // let coords = data.Coord.split(',')
        // //将修改的坐标值更新到配置文件
        // coords.forEach((coord: any) => {
        //     this.coordConfigMap.get(Number(coord.split(':')[0])).Value = Number(coord.split(':')[1])
        // });
        //将新的配置文件数据写入融合标定数据
        this.coordConfigMap.forEach((coordConfig: any) => {
            this.fusionCalibrationConfigModel.Coord.Content.push(coordConfig)
        });
        return this.fusionCalibrationConfigModel
    }
    
    //获取融合标定数据
    public Get = () => {
        this.fusionCalibrationConfigModel.Calibration.forEach((element:any) => {
            if(element.Title != '') {
                element.Coord.forEach((coord:any) => {
                    if(coord == '{}' || coord.Value == undefined) {
                        return
                    } else {  
                        //@ts-ignore
                        this.coordAnswer.value.push(this.calculateCoord(coord.Answer))
                    }
                });
                element.Posture.forEach((posture:any) => {
                    if(posture == '{}' || posture.Value == undefined) {
                        return
                    } else {
                        posture.Answer = parseFloat(posture.Answer).toFixed(3);
                    }
                });
            }
        });
       return this.fusionCalibrationConfigModel
    }

    //更新融合标定数据
    // public Update = () => {
    //     return this.fusionCalibrationConfigModel
    // }

    //同步坐标标定
    public SyncCoord = (data:any) => {
        return this.fusionCalibrationConfigModel.Coord
    }

    //标定
    public Calibration = (data:any) => {
        return this.fusionCalibrationConfigModel.Calibration
    }

    //标定评价
    // public Appraise = (data:any) => {
    public Appraise = () => {
        let score = 0
        let totle = 24
        this.fusionCalibrationConfigModel.Calibration.forEach((calibration: any) => {
            
            //计算坐标标定
            calibration.Coord.forEach((coord: any,index:any) => {  
                // console.log('coord.Value',coord.Value,coord.Answer);
                if(coord.Value != undefined && coord.Value != '') {
                    // console.log('index',index);
                    coord.State = coord.Value == this.calculateCoord(coord.Answer) ? 1 : 2                            
                    score +=  coord.State == 1 ? 1 : 0
                    // totle++;
                }                        
            });
            //计算姿态标定
            calibration.Posture.forEach((posture: any,index:any) => {
                // console.log('posture',posture.Value);
                
                if(posture.Value != '' && posture.Value != undefined) {
                    // console.log('index',index);
                    
                    posture.State = posture.Value == posture.Answer ? 1 : 2
                    score +=  posture.State == 1 ? 1 : 0
                    // totle++;
                }                    
            });
        });        
        console.log('正确的个数score',score);
        console.log('题目总数totle',totle);  
        console.log('score/totle*100',score/totle*100);
        console.log('四舍五入结果',Math.round(score/totle*100));
        ExamComponent.Default.FusCalScore.value = Math.round(score/totle*100)
        return Math.round(score/totle*100)
    }

    //计算坐标值
    public calculateCoord = (data:any) => {
        // return this.coordConfigMap.get(Number(data.split(',')[0])).Value - this.coordConfigMap.get(Number(data.split(',')[1])).Value
        // console.log('计算坐标值data',data);
        // console.log('第一个值',this.coordConfigMap.get(Number(data.split(',')[0])).Positon);
        // console.log('第二个值',this.coordConfigMap.get(Number(data.split(',')[1])).Positon);
        
        if(data != undefined && data != '') {
            let answer = (this.coordConfigMap.get(Number(data.split(',')[0])).Positon) - (this.coordConfigMap.get(Number(data.split(',')[1])).Positon)
            //@ts-ignore
            answer = parseFloat(answer).toFixed(3)
            // console.log('answer',typeof(answer),answer);
            return answer
        }
    }
}